'use strict'
const path = require('path')
const CompressionPlugin = require('compression-webpack-plugin')// 引入gzip压缩插件
const webpack = require('webpack')

function resolve (dir) {
  return path.join(__dirname, dir)
}
// If your port is set to 80,
// use administrator privileges to execute the command line.
// For example, Mac: sudo npm run
// You can change the port by the following method:
// port = 9527 npm run dev OR npm run dev --port = 9527

// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
  /**
   * You will need to set publicPath if you plan to deploy your site under a sub path,
   * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
   * then publicPath should be set to "/bar/".
   * In most cases please use '/' !!!
   * Detail: https://cli.vuejs.org/config/#publicpath
   */
  publicPath: '/',
  outputDir: 'dist',
  // assetsDir: '../../static/admin',
  // assetsDir: '/',
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: process.env.NODE_ENV === 'development',
  configureWebpack: {
    plugins: [
      new CompressionPlugin({
        algorithm: 'gzip',
        test: /\.js$|\.html$|\.css/, // 匹配文件名
        threshold: 10240, // 对超过10kb的数据进行压缩
        deleteOriginalAssets: false, // 是否删除原文件
        minRatio: 0.8
      }),
      new webpack.ProvidePlugin({
        'window.Quill': 'quill/dist/quill.js',
        'Quill': 'quill/dist/quill.js'
      })
    ]
  },
  devServer: {
    open: true,
    host: '0.0.0.0',
    port: process.env.VUE_APP_PORT || 8080,
    https: false,
    hotOnly: false
  },
  chainWebpack (config) {
    config.plugins.delete('preload') // TODO: need test
    config.plugins.delete('prefetch') // TODO: need test
    // if (process.env.NODE_ENV === 'production') { // npm run build --report
    //   config.plugin('webpack-bundle-analyzer')
    //     .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
    //     .end()
    // }
    config.module
      .rule('svg')
      .exclude.add([resolve('src/icons'), resolve('node_modules/zcsd-core/icons')])
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add([resolve('src/icons'), resolve('node_modules/zcsd-core/icons')])
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    // set preserveWhitespace
    config.module
      .rule('vue')
      .use('vue-loader')
      .loader('vue-loader')
      .tap(options => {
        options.compilerOptions.preserveWhitespace = true
        return options
      })
      .end()
    config.plugin('html').tap((args) => {
      args[0].title = process.env.VUE_APP_SERVE_NAME
      return args
    })
  },
  css: {}
}
